package com.worklog.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.worklog.mapper.UserMapper;
import com.worklog.mapper.UserRoleMapper;
import com.worklog.model.User;
import com.worklog.model.UserRole;
import com.worklog.service.UserService;
import com.worklog.util.ResultMap;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.util.StringUtil;

@Service("userService")
public class UserServiceImpl extends BaseService<User> implements UserService {
	@Resource
	private UserRoleMapper userRoleMapper;

	@Resource
	private UserMapper userMapper;

	@Override
	public PageInfo<User> selectByPage(User user, int start, int length) {
		int page = start / length + 1;
		Example example = new Example(User.class);
		Example.Criteria criteria = example.createCriteria();
		if (StringUtil.isNotEmpty(user.getUsername())) {
			criteria.andLike("username", "%" + user.getUsername() + "%");
		}
		if (user.getId() != null) {
			criteria.andEqualTo("id", user.getId());
		}
		if (user.getEnable() != null) {
			criteria.andEqualTo("enable", user.getEnable());
		}
		// 分页查询
		PageHelper.startPage(page, length);
		List<User> userList = selectByExample(example);
		return new PageInfo<>(userList);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackFor = { Exception.class })
	public void delUser(Integer userid) {
		// 删除用户表
		mapper.deleteByPrimaryKey(userid);
		// 删除用户角色表
		Example example = new Example(UserRole.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo("userid", userid);
		userRoleMapper.deleteByExample(example);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackFor = { Exception.class })
	public void delUsers(List<Integer> ids) {
		Example example = new Example(User.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andIn("id", ids);
		mapper.deleteByExample(example);

		Example example2 = new Example(UserRole.class);
		Example.Criteria criteria2 = example2.createCriteria();
		criteria2.andIn("userid", ids);
		userRoleMapper.deleteByExample(example2);
	}

	@Override
	public PageInfo<User> queryAll(int page, int length) {
		PageHelper.startPage(page, length);
		List<User> userList = userMapper.queryAll();
		return new PageInfo<>(userList);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackFor = { Exception.class })
	public int save(User entity) {
		mapper.insertSelective(entity);
		int id = entity.getId();
		UserRole userRole = new UserRole();
		userRole.setUserid(id);
		userRole.setRoleid(entity.getRoleid());
		userRoleMapper.insertSelective(userRole);
		return id;
	}

	@Override
	public List<ResultMap<String, Object>> queryAllStatus() {
		return userMapper.queryAllStatus();
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackFor = { Exception.class })
	public int updateNotNull(User user) {
		int id = userMapper.updateByPrimaryKeySelective(user);

		UserRole userRole = new UserRole();
		userRole.setRoleid(user.getRoleid());
		userRole.setUserid(user.getId());

		Example example = new Example(UserRole.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo("userid", userRole.getUserid());

		userRoleMapper.updateByExampleSelective(userRole, example);
		return id;
	}

	@Override
	public User selectByUsername(String username) {
		User user = userMapper.selectByUsername(username);
		return user;
	}
}
